package com.example.administrator.baikangxing.activity;


import android.os.CountDownTimer;
import android.text.Editable;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.baikangxing.R;
import com.example.administrator.baikangxing.config.Constants;
import com.example.administrator.baikangxing.config.Url;
import com.example.administrator.baikangxing.utils.HttpUtil;
import com.example.administrator.baikangxing.utils.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

public class ForgotActivity extends BaseActivity implements View.OnClickListener {
    private boolean isHide = true;

    private TextView forgot_tv_submit,forgot_tv_get_code;
    private ImageView forgot_iv_eye;
    private EditText forgot_et_username, forgot_et_new_pass,forgot_et_code;
    private TimeCount time;

    @Override
    public int getContentViewLayoutId() {
        return R.layout.activity_forgot;
    }

    @Override
    public void initView() {
        setTitles("重置密码");
        base_ib_menu.setVisibility(View.INVISIBLE);
        forgot_et_code = findView(R.id.forgot_et_code);
        forgot_et_new_pass = findView(R.id.forgot_et_new_pass);
        forgot_et_username = findView(R.id.forgot_et_username);
        forgot_tv_get_code = findView(R.id.forgot_tv_get_code);
        forgot_iv_eye = findView(R.id.forgot_iv_eye);
        forgot_tv_submit = findView(R.id.forgot_tv_submit);
        forgot_tv_submit.setOnClickListener(this);
        forgot_iv_eye.setOnClickListener(this);
        forgot_tv_get_code.setOnClickListener(this);
        time = new TimeCount(6000,100);
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.forgot_iv_eye://密码可见
                if(isHide){
                    isHide = false;
                    forgot_et_new_pass.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    forgot_iv_eye.setImageResource(R.drawable.eye2);
                }else {
                    isHide = true;
                    forgot_et_new_pass.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    forgot_iv_eye.setImageResource(R.drawable.eye1);
                }
                final Editable ea = forgot_et_new_pass.getText();
                forgot_et_new_pass.setSelection(ea.length());
                break;
            case R.id.forgot_tv_submit://重置密码
                if(TextUtils.isEmpty(forgot_et_username.getText().toString().trim())){
                    ToastUtil.showToast("手机号不能为空");
                }
                if(TextUtils.isEmpty(forgot_et_new_pass.getText().toString().trim())){
                    ToastUtil.showToast("新密码不能为空");
                }
                if(TextUtils.isEmpty(forgot_et_code.getText().toString().trim())){
                    ToastUtil.showToast("验证码不能为空");
                }
                String[] values = {forgot_et_username.getText().toString().trim(), forgot_et_code.getText().toString().trim(), forgot_et_new_pass.getText().toString().trim()};
                HttpUtil.getInstance().postString(Url.reset_password_url, new String[]{"tel", "code", "newPassword"}, values, new HttpUtil.CallBackPostString() {
                    @Override
                    public void onSuccess(JSONObject successString) {
                        try {
                            String errorCode = successString.getString("errorCode");
                            if(errorCode.equals("0")){
                                ToastUtil.showToast("修改密码成功");
                            }else if(errorCode.equals("7")){
                                ToastUtil.showToast("重置密码失败");
                            }else if(errorCode.equals("5")){
                                ToastUtil.showToast("校验验证码失败");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFail(JSONObject failString) {

                    }

                    @Override
                    public void onProgress(long bytesWritten, long totalSize) {

                    }
                });
                break;
            case R.id.forgot_tv_get_code://获取验证码
                String user_name = forgot_et_username.getText().toString().trim();
                if(TextUtils.isEmpty(user_name) || user_name == null){
                    ToastUtil.showToast("手机号不能为空");
                }else {
                    HttpUtil.getInstance().postString(Url.get_code_url, new String[]{"tel", "appid", "ext"}, new String[]{forgot_et_username.getText().toString().trim(), Constants.APP_VERSION, "reset"}, new HttpUtil.CallBackPostString() {
                        @Override
                        public void onSuccess(JSONObject successString) {
                            try {
                                String errorCode = successString.getString("errorCode");
                                if(errorCode.equals("0")){
                                    ToastUtil.showToast("获取验证码成功");
                                    time.start();
                                }else if(errorCode.equals("3")){
                                    ToastUtil.showToast("该用户已注册，请直接登录");
                                }else if(errorCode.equals("2")){
                                    ToastUtil.showToast("电话号码错误,请重新输入");
                                }else if(errorCode.equals("4")){
                                    ToastUtil.showToast("获取验证码失败，请重试");
                                }else {
                                    ToastUtil.showToast(successString.getString("errorinfo"));
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onFail(JSONObject failString) {

                        }

                        @Override
                        public void onProgress(long bytesWritten, long totalSize) {

                        }
                    });
                }
                break;
            default:
                break;
        }
    }

    class TimeCount extends CountDownTimer {

        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            forgot_tv_get_code.setClickable(false);
            forgot_tv_get_code.setText("("+millisUntilFinished / 1000 +") 秒后可重新发送");
        }

        @Override
        public void onFinish() {
            forgot_tv_get_code.setText("重新获取验证码");
            forgot_tv_get_code.setClickable(true);

        }
    }
}
